home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Essentials / MacApp Documentation / MacApp.TECH$ Archives / 1989 / Jul 89 / W0008-Re Displaying Error-Jul89 < prev    next >
Encoding:
Text File  |  1991-03-06  |  3.3 KB  |  120 lines  |  [TEXT/GEOL]

  1. Item    8117919                         3-July-89        14:51
  2.  
  3. From:   CREMER.M                        Cremer, Mike
  4.  
  5. To:     KEMINK1                         Kemink, Joost
  6.  
  7. cc:     MACAPP.TECH$                    MACAPP Tech
  8.  
  9. Sub:    Re: Displaying Error Messages
  10.  
  11. Joost And Geoff,
  12.  
  13. Below is my fast-and-dirty solution to personalized MacApp error messages.
  14. Please note that this is probably neither the official nor sanctioned way to
  15. handle error messages, i.e. do not be surprised if somebody flames me for being
  16. so incompetent...
  17.  
  18. In general, the simple way out is to override TApplication.ShowError like this:
  19.  
  20. { begin code }
  21.  
  22. PROCEDURE TMyApplication.ShowError(error: OSErr; message: Longint); OVERRIDE;
  23.  
  24.    CONST
  25.    kMsgCmdErr   = msgCmdErr DIV $10000;  { needed to check to make sure }
  26.    kMsgAlert     = msgAlert  DIV $10000;  { the error message was generated }
  27.    kMsgLookup   = msgLookup DIV $10000;  { by us and not MacApp }
  28.  
  29. TYPE
  30.   { ErrOverlay gives easy access to hi and lo words of the error message }
  31.    ErrOverlay = RECORD
  32.        CASE Integer OF
  33.        1: (highMsg : Integer;
  34.            lowMsg  : Integer);
  35.        2: (message : Longint);
  36.    END; {ErrOverlay}
  37.  
  38.    VAR
  39.    msg      : ErrOverlay;
  40.    cmdStr    : Str255;
  41.    msgStr    : Str255;
  42.    errStr    : Str255;
  43.  
  44.    BEGIN {ShowError}
  45.  
  46.    msg := ErrOverlay(message);
  47.  
  48.    CASE msg.highMsg OF
  49.  
  50.     { pass MacApp errors on to MacApp }
  51.        kMsgCmdErr  :   INHERITED ShowError(error, message);
  52.        kMsgAlert   :   INHERITED ShowError(error, message);
  53.        kMsgLookup  :   INHERITED ShowError(error, message);
  54.  
  55.     { handle our own errors }
  56.        Otherwise
  57.          BEGIN
  58.         { this replaces the command and message strings, as well as }
  59.         { displaying the Mac OSErr number.  Change as needed to suit }
  60.         { your tastes. }
  61.         GetIndString(cmdStr, msg.highMsg, msg.lowMsg);
  62.  
  63.         { see below about this next line }
  64.            GetIndString(msgStr, kMyMessageStringList, ErrToStrID(error));
  65.  
  66.            NumToString(error, errStr);
  67.            errStr := Concat('(', errStr, ')');
  68.  
  69.            ParamText(msgStr, errStr, cmdStr, '');
  70.  
  71.            StdAlert(phGenError);
  72.          END;
  73.    END; {CASE highMsg}
  74.    END; {ShowError}
  75.  
  76. { end code }
  77.  
  78. Then, when using CatchFailures, you need something like this:
  79.  
  80. { begin code }
  81.  
  82. PROCEDURE TMyObject.ErrorProneMethod(parms);
  83.  
  84.   VAR
  85.     fi  : FailInfo;
  86.  
  87.   PROCEDURE HandleAnError(error: OSErr; message: Longint);
  88.        VAR
  89.        newMsg   : ErrOverlay;
  90.  
  91.     BEGIN {HandleAnError}
  92.            newMsg.highMsg := kMyErrorStringList;  { STR# resource ID }
  93.            newMsg.lowMsg := kMyErrorMessage;      { index into string list }
  94.            FailNewMessage(error, message, newMsg.message);
  95.       { other cleanups as necessary }
  96.        END; {HandleAnError}
  97.  
  98.     BEGIN {ErrorProneMethod}
  99.       ...
  100.       CatchFailures(fi, HandleAnError);
  101.       Foo(bar);
  102.       Success(fi);
  103.       ...
  104.     END; {ErrorProneMethod}
  105.  
  106. { end code }
  107.  
  108. Note that ErrToStrID above is some function that translates a Mac OSErr into
  109. some kind of resonable string id#.  I use a big CASE statement to map OSerr's
  110. to an index in a single STR# resource (kMyMessageStringList).
  111.  
  112. This code will allow you to define your own error codes, and trap them at
  113. ShowAlert, but leaves the MacApp error handlers intact.  Anyway, it works for
  114. me.
  115.  
  116. Hope this helps.
  117.  
  118. $mike cremer
  119.  
  120.